package example.common.property;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DatePropertyEditor extends PropertyEditorSupport {
	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		if (text == null || "".equals(text)) this.setValue(new Date());
		try { 
            setValue(new SimpleDateFormat("dd/MM/yyyy").parse(text)); 
        } catch(ParseException e) { 
            setValue(null);
        }
	}
	
	@Override
	public String getAsText() {
		return new SimpleDateFormat("dd/MM/yyyy").format((Date) getValue());
	}
}
